package com.future.bantang.fragment;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.future.bantang.R;
import com.future.bantang.adapter.FraSearchSingleProductListAdapter;
import com.future.bantang.adapter.FraSearchSingleProductSubListAdapter;
import com.future.bantang.constants.BanTang;
import com.future.bantang.constants.URL;
import com.future.bantang.entity.SearchCategoryListEntity;
import com.future.bantang.utils.JsonUtils;
import com.future.bantang.utils.L;
import com.future.bantang.utils.NetUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.ResType;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ResInject;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.List;

/**
 * Created by zzh on 2016/1/15.
 * 搜索分类--> 单品
 *
 */

public class SearchSingleProductFragment extends BaseFragment{

    public static final String TAG="SearchSingleProductFragment";

    //一级类目列表
    @ViewInject(R.id.list_fra_search_single_product_category)
    private ListView categoryList;

    //二级类目
    @ViewInject(R.id.grid_fra_search_single_product_content)
    private GridView productGrid;

    @ResInject(id = R.string.network_error,type = ResType.String)
    private String networkErro;

    //单品数据接口
    private String categoryUrl;

    //分类数据
    private List<SearchCategoryListEntity.DataEntity> dataEntities;

    //一级类目
    private FraSearchSingleProductListAdapter categorylistAdapter;

    //二级类目
    private FraSearchSingleProductSubListAdapter subListAdapter;




    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        categoryUrl=URL.CATEGORY_LIST_URL;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_search_single_product,container, false);
    }


    @Override
    protected void initView(View view) {

        //一级目录
        categorylistAdapter = new FraSearchSingleProductListAdapter(getActivity());
        categoryList.setAdapter(categorylistAdapter);

        //二级目录
        subListAdapter=new FraSearchSingleProductSubListAdapter(getActivity());
        productGrid.setAdapter(subListAdapter);


        categoryList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    //改变被点击的item的背景，字体颜色
                    categorylistAdapter.setSelected(i);
                    categorylistAdapter.notifyDataSetChanged();

                    //加载显示子类数据
                    loadSubclassDatas(i);
            }
        });




    }

    private void loadSubclassDatas(int i) {
        subListAdapter.setDatas(dataEntities.get(i).getSubclass());
    }

    @Override
    protected void loadDatas() {
        NetUtils.RequestParamsBuilder builder=new NetUtils.RequestParamsBuilder();

        /*
        client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&is_new=1
         */
        RequestParams params = builder.addPostParameter(BanTang.CLIENT_ID_KEY, BanTang.CLIENT_ID_VALUE)
                                     .addPostParameter(BanTang.CLIENT_SECRET_KEY, BanTang.CLIENT_SECRET_VALUE)
                                     .addPostParameter("is_new", "1").build();

        NetUtils.doPost(categoryUrl, params, new RequestCallBack<String>() {

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                L.d(TAG,responseInfo.result);
                try {
                    SearchCategoryListEntity searchCategoryListEntity = JsonUtils.perse(responseInfo.result, SearchCategoryListEntity.class);
                    dataEntities = searchCategoryListEntity.getData();

                    if (dataEntities.size()>0) {
                        categorylistAdapter.setDatas(dataEntities);
                        subListAdapter.setDatas(dataEntities.get(0).getSubclass());

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                Toast.makeText(getActivity(), networkErro, Toast.LENGTH_SHORT).show();
            }
        });

    }

}
